package cn.fresh.xuanyuan.module.system.controller.admin.notice;

import cn.fresh.xuanyuan.framework.common.pojo.CommonResult;
import cn.fresh.xuanyuan.framework.common.pojo.PageResult;
import cn.fresh.xuanyuan.framework.common.validation.ValidatorUtils;
import cn.fresh.xuanyuan.framework.common.validation.group.AddGroup;
import cn.fresh.xuanyuan.framework.common.validation.group.UpdateGroup;
import cn.fresh.xuanyuan.module.system.dal.dataobject.wx.TemplateConfigDO;
import cn.fresh.xuanyuan.module.system.model.form.AddWXTemplateConfigForm;
import cn.fresh.xuanyuan.module.system.model.request.WXSendRequest;
import cn.fresh.xuanyuan.module.system.service.wx.WXSubscribeService;
import cn.fresh.xuanyuan.module.system.service.wx.WXTemplateConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static cn.fresh.xuanyuan.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 模板消息")
@RestController
@RequestMapping("/system/template")
public class TemplateConfigController {

    @Resource
    private WXTemplateConfigService wxTemplateConfigService;

    @Resource
    private WXSubscribeService wxSubscribeService;


    @PostMapping(value = "/addWXTemplate")
    @Operation(summary = "添加微信模板消息配置")
    @PreAuthorize("@ss.hasPermission('system:wx:template:add')")
    public CommonResult<String> addWXTemplate(@RequestBody AddWXTemplateConfigForm form) {
        ValidatorUtils.validateEntity(form, AddGroup.class);
        wxTemplateConfigService.addTemplateConfig(getLoginUserId(), form);
        return CommonResult.success("success");
    }

    @PostMapping(value = "/editWXTemplate")
    @Operation(summary = "修改微信模板消息配置")
    @PreAuthorize("@ss.hasPermission('system:wx:template:edit')")
    public CommonResult<String> editWXTemplate(@RequestBody AddWXTemplateConfigForm form) {
        ValidatorUtils.validateEntity(form, UpdateGroup.class);
        wxTemplateConfigService.editTemplateConfig(getLoginUserId(), form);
        return CommonResult.success("success");
    }

    @GetMapping(value = "/getWXTemplateList")
    @PreAuthorize("@ss.hasPermission('system:wx:template:list')")
    public CommonResult<PageResult<TemplateConfigDO>> getWXTemplateList(@RequestParam(name = "pageNo") Integer pageNo, @RequestParam(name = "pageSize") Integer pageSize) {
        PageResult<TemplateConfigDO> page = wxTemplateConfigService.getPage(pageNo, pageSize);
        return CommonResult.success(page);
    }

    @PostMapping(value = "/sendTemplate")
    @Operation(summary = "发送微信模板消息")
    @PreAuthorize("@ss.hasPermission('system:wx:template:send')")
    public CommonResult<String> sendTemplate(@RequestBody WXSendRequest request) {
        ValidatorUtils.validateEntity(request, AddGroup.class);
        wxSubscribeService.send(getLoginUserId(), request);
        return CommonResult.success("success");
    }
}
